extends HBoxContainer

@export var stats: Stats

@onready var health_bar: TextureProgressBar = $HealthBar
@onready var eased_health_bar: TextureProgressBar = $HealthBar/EasedHealthBar

func _ready() -> void:
	if stats == null:
		stats = Game.player_stats

	stats.health_changed.connect(update_health)
	update_health(true)
	
	tree_exited.connect(func ():
		stats.health_changed.disconnect(update_health)
	)

func update_health(skip_anim := false) -> void:
	var percentege := stats.health / float(stats.max_health)
	health_bar.value = percentege
	if skip_anim:
		eased_health_bar.value = percentege
	else:
		# 补间动画
		create_tween().tween_property(eased_health_bar, "value", percentege, 0.3)
